package com.zhengaz.common.log.aspect;


import cn.hutool.core.util.StrUtil;
import com.zhengaz.common.core.entity.R;
import com.zhengaz.common.log.annotation.SysLog;
import com.zhengaz.common.log.service.AsyncLogService;
import lombok.AllArgsConstructor;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;

import javax.annotation.Resource;

/**
 * @author zhengzg
 * @create 2023-05-22 17:45
 */
@Slf4j
@Aspect
@AllArgsConstructor
public class SysLogAspect {

	@Resource
	private AsyncLogService asyncLogService;

    @Around("@annotation(sysLog)")
	@SneakyThrows
    public Object around(ProceedingJoinPoint point, SysLog sysLog) {
		Object proceed = point.proceed();
		if (proceed instanceof R) {
			R<?> result = (R<?>) proceed;
			if (result.getCode() == 200 && StrUtil.equals(result.getMsg(), "success")) {
				// 记录数据
				// 创建存储实体

				// 发送异步日志
				asyncLogService.saveLog();
			}
		}
		return proceed;
    }
}
